<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use vquery;
use app\model\zhanghao as zh;
use app\model\bill as bill;

/**
 * Description of Num
 *
 * @author Administrator
 */
class Num extends Check {

    public function __construct() {
        parent::__construct();
        $left = controller("index");
        $left->left();
    }

    public function person() {
        $uid = session("u_id"); //取出用户ID
        $pagenow = input("get.page"); //获取当前分页页数
        $list = db("zhanghao")->where("z_uid=$uid")->order("z_zcsj")->paginate(10);
        $page = $list->render();
        $this->assign('page', $page);
        $title = "Dengeki Bunko Num";
        session("cue", "个人账号");
        $this->assign("title", $title);
        $this->assign("list", $list);
        $this->assign("pagenow", $pagenow);
        return $this->fetch();
    }

//添加账号页面
    public function addnum() {
        $title = "手动入账";
        session("cue", "全部账单");
        $mlist = db("mode")->select();
        $this->assign("title", $title);
        $this->assign("mlist", $mlist);
        return $this->fetch();
    }

//新增账单
    public function save() {

        $data = input("post.");
        //$data['z_uid'] = $uid;
        $rule = [
            'account_info|收款帐户信息' => 'require',
            'account_name|收款帐户名称' => 'require',
            'time|时间' => 'require',
            'income_pattern|收入方式' => 'require',
            'money|收入金额' => 'require',
        ];
        $message = [
            'account_info.require' => '收款帐户信息不能为空！',
            'account_name.require' => '收款帐户名称不能为空！',
            'time.require' => '时间不能为空！',
            'income_pattern.require' => '收入方式不能为空！',
            'money.require' => '收入金额不能为空！',
        ];
//自定义规则
        $res = $this->validate($data, $rule, $message);

        if (true !== $res) {

            return ["status" => -1, 'message' => $res];
        }
        $sj = $data['time'];
        //$uid = session("u_id"); //取出用户ID
        $data['time'] = strtotime($sj);

        //$money = input("post.money"); 未处理


        if (bill::create($data)) {
            return ["status" => 1, 'message' => "保存成功！"];
        } else {
            return ["status" => 2, 'message' => "保存失败！"];
        }
    }

    public function doaddnum() {
        $data = input("post.");

        if (db("notice")->where("id=1")->update($data)) {
            return ["status" => 1, 'message' => "保存成功！"];
        } else {
            return ["status" => 2, 'message' => "保存失败,内容无变化！"];
        }
    }

    //删除账号

    public function dodelete() {
        $id = input("ids");
        $page = input("page");
        //$type = input("type");
        if (bill::destroy($id)) {
            $this->redirect("/allbill?page=$page");
        } else {
            return "<script>alert('删除失败,可能已经删除！');window.location.href='/allbill';</script>";
        }
    }

    //修改页面
    public function update() {
        $zid = input("zid");
        //$type = input("type");
        $page = input("page");
        $list = bill::get($zid);
        // var_dump($list);
        $mlist = db("mode")->select();
        $this->assign("mlist", $mlist);
        $title = "修改账单";
        session("cue", "全部账单");
        $this->assign("title", $title);
        $this->assign("list", $list);
        $this->assign("page", $page);
        //$this->assign("type", $type);
        return $this->fetch();
    }

    //
    public function alertview() {
        $id = input("id");
        $list = bill::get($id);
        $plist = db("parameter")->find(1); //参数设置
        $this->assign("list", $list);
        $this->assign("plist", $plist);
        return $this->fetch();
    }

    //执行修改方法
    public function doupdate() {
        $data = input("post.");
        $rule = [
            'account_info|收款帐户信息' => 'require',
            'account_name|收款帐户名称' => 'require',
            'time|时间' => 'require',
            'income_pattern|收入方式' => 'require',
            'money|收入金额' => 'require',
        ];
        $message = [
            'account_info.require' => '收款帐户信息不能为空！',
            'account_name.require' => '收款帐户名称不能为空！',
            'time.require' => '时间不能为空！',
            'income_pattern.require' => '收入方式不能为空！',
            'money.require' => '收入金额不能为空！',
        ];
//自定义规则
        $res = $this->validate($data, $rule, $message);

        if (true !== $res) {

            return ["status" => -1, 'message' => $res];
        }
        $sj = $data['time'];
        //$uid = session("u_id"); //取出用户ID
        $data['time'] = strtotime($sj);
        if (bill::update($data)) {
            return ["status" => 1, 'message' => "保存成功！"];
        } else {
            return ["status" => 2, 'message' => "修改失败！"];
        }
    }

    //所有账号

    public function allnum() {
        $where = "";
        $where2 = "";
        $where3 = "";
        $v = input("v");
        $time = input("time");

        $time = base64_decode($time);
        //var_dump($time);

        $mstart = input("mstart");
        //var_dump($mstart);
        $mend = input("mend");

//        if($mend>$mstart)
//        {
//            return false;
//        }
        $info = input("info");
        if ($v != "") {
            switch ($v) {
                case "wxpay":
                    $where['income_pattern'] = "微信";
                    break;
                case "alipay":
                    $where['income_pattern'] = "支付宝";
                    break;
                case "bank":
                    $where['income_pattern'] = "银行短信";
                    break;
            }
        }
        if ($time != "") {
            $timearr = explode("—", $time);
            $start = strtotime($timearr[0]);
            //var_dump($start);
            $end = strtotime($timearr[1]);
            //var_dump($end);
            $where['time'] = array('between', array($start, $end));
        }
        if ($mstart != "") {
            //$where['money'] = array('egt', "521");
            $where2 = "money>=$mstart";
        }
        if ($mend != "") {
            $where3 = "money<=$mend";
        }
        if ($info != "") {
            $where['drawee'] = ['like', '%' . $info . '%'];
        }
        $pagenow = input("get.page"); //获取当前分页页数

        $mlist = db("mode")->select();


        $list = db("bill")->where($where)->where($where2)->where($where3)->order("id desc")->paginate(10, false, ['query' => request()->param()]);
        //var_dump($where);
        $page = $list->render();
        $this->assign('page', $page);
        $title = "全部账单";
        session("cue", "全部账单");
        $this->assign("title", $title);
        $this->assign("list", $list);
        $this->assign("mlist", $mlist);
        $this->assign("pagenow", $pagenow);
        //-----------------------------
        $this->assign("time", $time);
        $this->assign("mstart", $mstart);
        $this->assign("mend", $mend);
        $this->assign("info", $info);

        $this->assign("v", $v);
        return $this->fetch();
    }

}
